//
//  HEPreferPopView.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/5/7.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEPreferPopView.h"
#import "UICollectionViewLeftAlignedLayout.h"
#import "ZQSearchNormalCell.h"

/// 标题栏高度（包括确定/取消，和年/月/日）
#define KAlterHeight 350

/// 主题颜色
#define kThemeColor [UIColor blueColor]

@interface HEPreferPopView ()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

// 中间标题
@property (nonatomic, strong) UILabel *titleLabel;

// 蒙层视图
@property (nonatomic, strong) UIView *maskView;

// 弹出背景视图
@property (nonatomic, strong) UIView *alertView;


@property (nonatomic, strong) QMUIFillButton *finishBtn;

@property (strong, nonatomic) UICollectionView *collectionView;

@end

@implementation HEPreferPopView



- (void)show {
     
    [self initUI];
    
    [self setupData];
     
    UIWindow *keyWindow = BRGetKeyWindow();
    [keyWindow addSubview:self];
    // 动画前初始位置
    CGRect rect = self.alertView.frame;
    rect.origin.y = self.bounds.size.height;
    self.alertView.frame = rect;
    // 弹出动画
    self.maskView.alpha = 1;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y -= BR_BOTTOM_MARGIN + KAlterHeight;
        self.alertView.frame = rect;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)initUI {
    self.frame = [UIScreen mainScreen].bounds;
    // 设置子视图的宽度随着父视图变化
    self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    [self addSubview:self.maskView];
    
    [self addSubview:self.alertView];
    
    [self.alertView addSubview:self.titleLabel];

    [self.alertView addSubview:self.finishBtn];

}

- (void)setupData {
    self.titleLabel.text = _model.name;
    
    NSString *date = nil;
    
    if (_model) {
        date = [NSString stringWithFormat:@"%@ ~ %@", [NSDate timestampToDate:_model.startTime formaterMode:NSDateFormaterModeDay], [NSDate timestampToDate:_model.endTime formaterMode:NSDateFormaterModeDay]];
    }
    
    [self addInitCollectView];
    
    // middle
    UILabel *mainCntLb = [UILabel new];
    mainCntLb.text = @"优惠时间：";
    mainCntLb.font = DYBlodFont(18);
    mainCntLb.textColor = CNavLightBlackColor;
    [self.alertView addSubview: mainCntLb];
    
    UILabel *audioResLb = [UILabel new];
    audioResLb.text = @"优惠范围：";
    audioResLb.font = DYBlodFont(18);
    audioResLb.textColor = CNavLightBlackColor;
    [self.alertView addSubview: audioResLb];
    
    UILabel *remarkTitlLb = [UILabel new];
    remarkTitlLb.text = @"相关优惠：";
    remarkTitlLb.font = DYBlodFont(18);
    remarkTitlLb.textColor = CNavLightBlackColor;
    [self.alertView addSubview: remarkTitlLb];
 
    
    UILabel *_mainCntLb = [UILabel new];
    _mainCntLb.text = nullToDef(date);
    _mainCntLb.font = DYFont(18);
    _mainCntLb.textColor = CNavLightBlackColor;
    _mainCntLb.numberOfLines = 0;
    [self.alertView addSubview:_mainCntLb];
    
    UILabel *_audioResLb = [UILabel new];
    _audioResLb.text = [HEEnums activeGoodToText: _model.activiGoods];
    _audioResLb.font = DYFont(18);
    _audioResLb.textColor = CNavLightBlackColor;
    _audioResLb.numberOfLines = 0;
    [self.alertView addSubview:_audioResLb];
    
    
    [mainCntLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(20);
        make.left.equalTo(self.titleLabel.mas_left);
    }];
    [mainCntLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
 
    [audioResLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_mainCntLb.mas_bottom).offset(10);
        make.left.equalTo(self.titleLabel.mas_left);
    }];
    [audioResLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [remarkTitlLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_audioResLb.mas_bottom).offset(10);
        make.left.equalTo(self.titleLabel.mas_left);
    }];
    [remarkTitlLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    
    [_mainCntLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(20);
        make.left.equalTo(mainCntLb.mas_right);
        make.right.equalTo(self.alertView).offset(-10);
    }];
    
    [_audioResLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_mainCntLb.mas_bottom).offset(10);
        make.left.equalTo(audioResLb.mas_right);
        make.right.equalTo(self.alertView).offset(-10);
    }];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(remarkTitlLb.mas_bottom).offset(5);
        make.left.equalTo(self.alertView);
        make.right.equalTo(self.alertView);
        make.bottom.equalTo(self.finishBtn.mas_top).offset(-20);
    }];
}

#pragma mark - collectionView
- (void)addInitCollectView{
    UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.alwaysBounceVertical = YES;
    [self.alertView addSubview:_collectionView];
    [self.collectionView registerClass:[ZQSearchNormalCell class] forCellWithReuseIdentifier: @"ZQSearchNormalCell"];

}

#pragma mark -
#pragma mark - 蒙层视图
- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2f];
        // 设置子视图的大小随着父视图变化
        _maskView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _maskView.userInteractionEnabled = YES;
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapMaskView:)];
        [_maskView addGestureRecognizer:myTap];
    }
    return _maskView;
}

#pragma mark - 弹框视图
- (UIView *)alertView {
    if (!_alertView) {
        _alertView = [[UIView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height - KAlterHeight - BR_BOTTOM_MARGIN, self.bounds.size.width,  BR_BOTTOM_MARGIN + KAlterHeight)];
        _alertView.backgroundColor = [UIColor whiteColor];
        _alertView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        [_alertView hg_setCornerOnTopWithRadius:10];
    }
    return _alertView;
}

#pragma mark - 中间标题label
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, self.bounds.size.width-30, 44)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = DYBlodFont(22);
        _titleLabel.textColor = CNavBgColor;
        _titleLabel.text = @"-";
        [self.alertView addSubview:_titleLabel];

        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.alertView).offset(25);
            make.left.equalTo(self.alertView).offset(15);
            make.right.equalTo(self.alertView).offset(-15);
        }];
    }
    return _titleLabel;
}

- (void)seeProtocoClick {
    
}

- (QMUIFillButton *)finishBtn {
    if (!_finishBtn) {
        _finishBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
        _finishBtn.titleLabel.font = UIFontMake(16);
        [_finishBtn setTitle:@"知道了" forState:UIControlStateNormal];
        [self.alertView addSubview: _finishBtn];
        
        [_finishBtn addTarget:self action:@selector(finishClick) forControlEvents:UIControlEventTouchUpInside];
        
        [_finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.alertView.mas_bottom).mas_offset(-30);
            make.width.offset(130);
            make.height.offset(40);
            make.centerX.equalTo(self.alertView);
        }];
    }
    return _finishBtn;
}

#pragma mark -

- (void)finishClick {
    [self dismiss];
}

#pragma mark - 点击蒙层视图事件
- (void)didTapMaskView:(UITapGestureRecognizer *)sender {
    [self dismiss];
}

#pragma mark - 关闭选择器视图
- (void)dismiss {
    // 关闭动画
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y +=  BR_BOTTOM_MARGIN + KAlterHeight;
        self.alertView.frame = rect;
        self.maskView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _model.KgoodsDs.count;
}
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZQSearchNormalCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZQSearchNormalCell" forIndexPath:indexPath];
  
    cell.title = self.model.KgoodsDs[indexPath.item].name;

    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *string = self.model.KgoodsDs[indexPath.item].name;
    
    CGSize size = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options: NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: DYFont(16)} context:nil].size;
    
    CGFloat width = size.width + 15 * 2;
    
    if (width >= (KScreenWidth-40)) {
        width = KScreenWidth-40;
    }
    
    CGSize rSize = CGSizeMake(width, 34);
    
    return rSize;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(15, 20, 0, 20);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10.f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10.f;
}



@end
